/**
 * xiejunyu
 * 常用
 */
layui.define(function (exports) {
    var $ = layui.$
        , layer = layui.layer
        , table = layui.table
        , form = layui.form
        , view = layui.view
        , toolJ = {
            error: function ($msg) {
                layer.msg($msg, {"icon": 2});
            }
            , success: function ($msg) {
                layer.alert($msg, {"icon": 1});
            }
        /**
         * ajax请求
         * xiejunyu
         * @param obj
         * @param callback 成功回调
         */
            , ajax: function (obj, callback) {
                if (typeof obj == 'undefined') {
                    obj = {};
                }
                if (typeof obj.url == 'undefined' || !obj.url) {
                    obj.url = '/index/' + layui.router().path.join('/')
                }
                if (typeof obj.type == 'undefined') {
                    obj.type = 'POST';
                }
                if (typeof obj.data == 'undefined') {
                    obj.data = {};
                }
                $.ajax({
                    type: obj.type,
                    url: obj.url,
                    dataType: 'json',
                    data: obj.data,
                    success: function (data) {
                        layer.closeAll();
                        if (typeof data.data == "undefined") {
                            var res = {};
                        } else {
                            var res = data.data;
                        }
                        //登陆态失效
                        if (data.code == 1001) {
                            view.exit();
                        }
                        if (data.code == 1) {
                            if (typeof res.url != "undefined") {
                                if (data.msg) {
                                    layer.msg(data.msg, {"icon": 1, time: 800},function () {
                                        window.location.href = data.data.url;
                                    });
                                }else{
                                    window.location.href = data.data.url;
                                }
                            } else {
                                if (data.msg) {
                                    layer.msg(data.msg, {"icon": 1});
                                }
                                $('.search').click();
                            }
                        } else {
                            layer.alert(data.msg, {"icon": 2});
                        }
                        typeof callback === 'function' && callback(data);
                    },
                    error: function () {
                        layer.closeAll();
                        layer.alert('系统繁忙,请稍后再试!', {"icon": 2});
                    },
                    beforeSend: function () {
                        layer.load(2, {
                            shade: [0.5, '#fff']
                        });
                    }
                });
            }
        /**
         **xiejunyu
         * @param url 上传api
         * @param csvUrl 模板下载链接
         * @param callback成功回调
         */
            , upload: function (url,csvUrl, callback) {
                layer.closeAll();
                var id=new Date().getTime();
                layer.open({
                    title: '上传',
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: '<div style="width: 280px; margin: 0 auto; margin-top: 64px;">' +
                        '<button type="button" style="margin-right: 20px" class="layui-btn" id="layUploadBtn'+id+'"><i class="layui-icon"></i>上传文件</button>' +
                        '<a href="'+csvUrl+'" class="layui-btn layui-btn-primary"><i class="layui-icon"></i>下载模板</a>' +
                        '</div>'
                });
                //上传文件 指定允许上传的文件类型
                layui.upload.render({
                    elem: '#layUploadBtn'+id
                    , url: url
                    , accept: 'file' //普通文件
                    , exts: 'csv' //只允许上传csv文件
                    , done: function (res) {
                        layer.closeAll();
                        if (res.code == 1) {
                            if (res.msg) {
                                layer.msg(res.msg);
                            }
                        } else {
                            layer.alert(res.msg);
                        }
                        typeof callback === 'function' && callback(res);
                    }
                    , before: function () {
                        layer.alert('执行中,请勿做其他操作!');
                        layer.load(1, {
                            shade: [0.5, '#fff']
                        });
                    }
                    , error: function () {
                        layer.closeAll();
                        layer.alert('失败,请稍后再试!');
                    }
                });
            }
        }
    ;
    var url = '/index/' + layui.router().path.join('/');//api地址
    var host = window.location.href;
    var href = host.replace(/#/, "index");

    //监听搜索
    layui.form.on('submit(LAY-list-search)', function (data) {
        var field = data.field;
        //执行重载
        layui.table.reload('LAY-list', {
            where: field,
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
        layui.table.render(null, 'LAY-list');
    });
    //监听提交
    layui.form.on('submit(LAY-submit)', function (data) {
        toolJ.ajax({"data": data.field});
    })
    //监听回车 自动提交
    $(document).keydown(function (e) {
        if (e.which == 13) {
            //$('.LAY-submit').trigger('click');
            $('.search').trigger('click');
        }
    });
    //监听下载
    layui.form.on('submit(LAY-list-down)', function (data) {
        var field = data.field;
        field.down = 1;
        toolJ.ajax({"data": field});
    });
    //重置
    $('body').off('click.reset').on('click.reset','.reset',function () {
        window.location.reload();
    });
    //监听ajax执行
    $('body').on('click',".jy-act",function () {
        var that=$(this);
        var json={};
        json.data=JSON.parse(that.attr('jy-data'));
        json.url=that.attr('jy-href');
        if($(this).attr('jy-confirm')){
            layer.confirm(that.attr('jy-confirm'),function () {
                toolJ.ajax(json);
            })
        }else{
            toolJ.ajax(json);
        }
        return false;
    });
    //对外暴露的接口
    exports('toolJ', toolJ);
});